package com.tencent.cloud.iov.flow.view.impl;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.cloud.iov.flow.view.IContentViewContainer;

/**
 * Fragment 容器，实现流布局的视图设置
 *
 * @author showxiao
 * @date 2018/6/14
 */

public abstract class BaseFlowFragment extends Fragment implements IContentViewContainer {

    private ViewGroup mContainer;
    private IContentViewContainer mDelegate;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return getContainer();
    }

    @Override
    public void setContentView(int resId) {
        getDelegate().setContentView(resId);
    }

    @Override
    public void setContentView(View contentView) {
        getDelegate().setContentView(contentView);
    }

    @Override
    public void setContentView(View contentView, ViewGroup.LayoutParams params) {
        getDelegate().setContentView(contentView, params);
    }

    public ViewGroup getContainer() {
        if (mContainer == null) {
            mContainer = new FrameLayout(getActivity());
            mContainer.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        return mContainer;
    }

    private IContentViewContainer getDelegate() {
        if (mDelegate == null) {
            mDelegate = new FlowViewContainer(getContainer());
        }
        return mDelegate;
    }
}
